package com.itheima.ai.controller;

import com.itheima.ai.entity.vo.MessageVO;
import com.itheima.ai.repository.ChatHistoryRepository;
import com.itheima.ai.repository.InMemoryChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@RequestMapping("/ai/history")
@RestController
@RequiredArgsConstructor
@Slf4j
public class chatHistoryController {

    private final ChatHistoryRepository chatHistoryRepository;

    private final ChatMemory chatMemory;

    /**
     * 根据业务类型查询会话历史（chatID列表）
     */
    @GetMapping("/{type}")
    public List<String> chatHistoryList(@PathVariable("type") String type) throws Exception{
        return chatHistoryRepository.getChatIds(type);
    }

    /**
     * 根据会话ID查询会话历史记录（对话内容）
     */
    @GetMapping("/{type}/{chatId}")
    public List<MessageVO> getChatHistory(@PathVariable("type") String type,  @PathVariable("chatId") String chatId){
        List<Message> messages = chatMemory.get(chatId);
        log.info("获取到会话历史记录:{},{}",messages,chatId);
        return messages.stream().map(MessageVO::new).toList();
    }
}
